package com.cnsuning.jobtool.jobtools.jbossgenerator;

import com.cnsuning.jobtool.utils.ExcelUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class JbossGenerator {
    @Test
    public void start() throws IOException, InvalidFormatException {
        //读取文件
        String fileName = "src/main/java/com/cnsuning/jobtool/jobtools/jbossgenerator/datasource_config.xlsx";
        List<List<Object>> dataList = ExcelUtils.readExcelNoTitleOneSheet(new File(fileName), "Sheet1");
        if (CollectionUtils.isEmpty(dataList))
        {
            System.out.println("无配置");
            return;
        }
        generateDataSource(dataList);
        generateSecuritydomain(dataList);

    }

    private void generateDataSource(List<List<Object>> dataList) throws IOException {
        String sourceStr = "\t\t\t\t<datasource jndi-name=\"%s\" pool-name=\"%s\" enabled=\"true\" use-java-context=\"true\">\n" +
                "\t\t\t\t\t<connection-url>jdbc:mysql://%s/%s?useSSL=false</connection-url>\n" +
                "\t\t\t\t\t<driver-class>com.mysql.jdbc.Driver</driver-class>\n" +
                "\t\t\t\t\t<connection-property name=\"timerLevelForQueryTimeOut\">\n" +
                "\t\t\t\t\t\t2\n" +
                "\t\t\t\t\t</connection-property>\n" +
                "\t\t\t\t\t<connection-property name=\"loginTimeout\">\n" +
                "\t\t\t\t\t\t5\n" +
                "\t\t\t\t\t</connection-property>\n" +
                "\t\t\t\t\t<driver>com.mysql</driver>\n" +
                "\t\t\t\t\t<transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>\n" +
                "\t\t\t\t\t<url-delimiter>|</url-delimiter>\n" +
                "\t\t\t\t\t<pool>\n" +
                "\t\t\t\t\t\t<min-pool-size>30</min-pool-size>\n" +
                "\t\t\t\t\t\t<max-pool-size>150</max-pool-size>\n" +
                "\t\t\t\t\t\t<prefill>false</prefill>\n" +
                "\t\t\t\t\t\t<flush-strategy>FailingConnectionOnly</flush-strategy>\n" +
                "\t\t\t\t\t</pool>\n" +
                "\t\t\t\t\t<security>\n" +
                "\t\t\t\t\t\t<security-domain>%s</security-domain>\n" +
                "\t\t\t\t\t</security>\n" +
                "\t\t\t\t\t<validation>\n" +
                "\t\t\t\t\t\t<check-valid-connection-sql>SELECT 1</check-valid-connection-sql>\n" +
                "\t\t\t\t\t\t<validate-on-match>false</validate-on-match>\n" +
                "\t\t\t\t\t\t<background-validation>true</background-validation>\n" +
                "\t\t\t\t\t\t<background-validation-millis>60000</background-validation-millis>\n" +
                "\t\t\t\t\t</validation>\n" +
                "\t\t\t\t\t<timeout>\n" +
                "\t\t\t\t\t\t<idle-timeout-minutes>10</idle-timeout-minutes>\n" +
                "\t\t\t\t\t</timeout>\n" +
                "\t\t\t\t</datasource>\n";
        String fileName = "src/main/java/com/cnsuning/jobtool/jobtools/jbossgenerator/datasource.xml";
        String commonSecuritydomainName = "securitydomain_mysql_omsdDS";
        File targetFile = new File(fileName);
        if (targetFile.exists()) targetFile.delete();
        for (List<Object> rowData : dataList)
        {
            if (CollectionUtils.isEmpty(rowData)) continue;
            String id = (String) rowData.get(0);
            String poolName = (String) rowData.get(1);
            String jndiName = (String) rowData.get(2);
            String ip = (String) rowData.get(3);
            String dbName = (String) rowData.get(4);
            String userName = (String) rowData.get(5);
            String password = (String) rowData.get(6);
            FileUtils.writeStringToFile(targetFile,String.format(sourceStr,jndiName,poolName,ip,dbName,commonSecuritydomainName+id),"utf-8",true);
        }
    }

    private void generateSecuritydomain(List<List<Object>> dataList) throws IOException {
        String sourceStr = "\t\t\t\t<security-domain name=\"%s\" cache-type=\"default\">\n" +
                "\t\t\t\t\t<authentication>\n" +
                "\t\t\t\t\t\t<login-module code=\"SecureIdentity\" flag=\"required\">\n" +
                "\t\t\t\t\t\t\t<module-option name=\"username\" value=\"%s\"/>\n" +
                "\t\t\t\t\t\t\t<module-option name=\"password\" value=\"%s\"/>\n" +
                "\t\t\t\t\t\t</login-module>\n" +
                "\t\t\t\t\t</authentication>\n" +
                "\t\t\t\t</security-domain>\n";
        String fileName = "src/main/java/com/cnsuning/jobtool/jobtools/jbossgenerator/securitydomain.xml";
        String commonSecuritydomainName = "securitydomain_mysql_omsdDS";
        File targetFile = new File(fileName);
        if (targetFile.exists()) targetFile.delete();
        for (List<Object> rowData : dataList)
        {
            if (CollectionUtils.isEmpty(rowData)) continue;
            String id = (String) rowData.get(0);
            String poolName = (String) rowData.get(1);
            String jndiName = (String) rowData.get(2);
            String ip = (String) rowData.get(3);
            String dbName = (String) rowData.get(4);
            String userName = (String) rowData.get(5);
            String password = (String) rowData.get(6);
            FileUtils.writeStringToFile(targetFile,String.format(sourceStr,commonSecuritydomainName+id,userName,password),"utf-8",true);
        }
    }
}
